﻿using System;
using DAL;
using Ext.Net;

namespace ERP.Modules
{
    public partial class Liabilities_Expenditure : System.Web.UI.UserControl
    {
        #region[Define]
        readonly clsConnect Cls = new clsConnect();
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                GetData();
                GetCustomer();
            }
        }
        //Store
        protected void StoreLiabilities_Expenditure_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearch.Text = string.Empty;
            txtDateBeginFillter.Text = string.Empty;
            txtDateEndFillter.Text = string.Empty;
            SelectBoxLiabilitiesExpenditure.Text = string.Empty;
            GetData();
        }
        //Button
        protected void btnFillter_Click(object sender, DirectEventArgs e)
        {
            GetLiabilitiesFillterDate();
        }
        //Search
        protected void Search_Event(object sender, DirectEventArgs e)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@KEYWORDS", txtSearch.Text);
            var dtSearch = Cls.GetData("sp_Get_Liabilities_Expenditure");
            if (dtSearch != null && dtSearch.Rows.Count > 0)
            {
                StoreLiabilities_Expenditure.DataSource = dtSearch;
                StoreLiabilities_Expenditure.DataBind();
            }
            else
            {
                grLiabilities_Expenditure.GetStore().RemoveAll();
            }
        }
        //Select-box
        protected void SelectBoxLiabilitiesExpenditure_Change(object sender, DirectEventArgs e)
        {
            GetData();
        }
        #endregion

        #region[Method]
        private void GetData()
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@KEYWORDS", txtSearch.Text);
            var dtGetData = Cls.GetData("sp_Get_Liabilities_Expenditure");
            if (String.IsNullOrEmpty(SelectBoxLiabilitiesExpenditure.Text))
            {
                if (dtGetData != null && dtGetData.Rows.Count > 0)
                {
                    StoreLiabilities_Expenditure.DataSource = dtGetData;
                    StoreLiabilities_Expenditure.DataBind();
                }
                else
                {
                    grLiabilities_Expenditure.GetStore().RemoveAll();
                }
            }
            else
            {
                Cls.CreateNewSqlCommand();
                Cls.AddParameter("@PayerRecipientCode", SelectBoxLiabilitiesExpenditure.SelectedItem.Value);
                var dtFillterCustomer = Cls.GetData("sp_Get_Liabilities_Expenditure_Fillter_Customer");
                if (dtFillterCustomer != null && dtFillterCustomer.Rows.Count > 0)
                {
                    StoreLiabilities_Expenditure.DataSource = dtFillterCustomer;
                    StoreLiabilities_Expenditure.DataBind();
                }
                else
                {
                    grLiabilities_Expenditure.GetStore().RemoveAll();
                }
            }
        }
        private void GetLiabilitiesFillterDate()
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@DateBegin", txtDateBeginFillter.Text);
            Cls.AddParameter("@EndBegin", txtDateEndFillter.Text);
            var _dtFillter = Cls.GetData("sp_Get_Liabilities_Expenditure_Fillter");
            if (String.IsNullOrEmpty(txtDateBeginFillter.Text) && String.IsNullOrEmpty(txtDateEndFillter.Text))
            {
                var _dtGetLiabilities = Cls.GetData("sp_Get_Liabilities_Expenditure");
                if (_dtGetLiabilities != null && _dtGetLiabilities.Rows.Count > 0)
                {
                    StoreLiabilities_Expenditure.DataSource = _dtGetLiabilities;
                    StoreLiabilities_Expenditure.DataBind();
                }
                else
                {
                    grLiabilities_Expenditure.GetStore().RemoveAll();
                }
            }
            else
            {
                if (_dtFillter != null && _dtFillter.Rows.Count > 0)
                {
                    StoreLiabilities_Expenditure.DataSource = _dtFillter;
                    StoreLiabilities_Expenditure.DataBind();
                }
                else
                {
                    grLiabilities_Expenditure.GetStore().RemoveAll();
                }
            }
        }
        private void GetCustomer()
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@KeyWords",string.Empty);
            var _dtFillterCustomer = Cls.GetData("sp_tb_Customer_Search");
            if (_dtFillterCustomer != null && _dtFillterCustomer.Rows.Count > 0)
            {
                StoreLiabilitiesExpenditure.DataSource = _dtFillterCustomer;
                StoreLiabilitiesExpenditure.DataBind();
            }
            else
            {
                SelectBoxLiabilitiesExpenditure.GetStore().RemoveAll();
            }
        }
        #endregion
    }
}